# 使用例子
import sys
import time

from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import (QWidget, QApplication,
                             QVBoxLayout, QHBoxLayout,
                             QPushButton, QLabel, QSizePolicy, QSplashScreen)
from qt_material import apply_stylesheet

from gameWindow import GameWindow
from myParam import MyParam, GAME_STATUS
from descDialog import DescDialog


# 游戏参数构建
def initParam():
    return (MyParam.Builder()
            .setTimerMs(10)
            .setGameStatus(GAME_STATUS.GAME_PAUSE)
            .build())


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.myParam = initParam()
        self.initUi()

    # 初始化ui
    def initUi(self):
        self.setWindowTitle("snake")
        self.setWindowIcon(QIcon("img/snake.png"))
        self.setFixedSize(800, 680)

        self.vl = QVBoxLayout(self)
        self.hl = QHBoxLayout()
        self.btn_start_game = QPushButton('开始游戏')
        self.btn_start_game.setFocusPolicy(Qt.NoFocus)
        self.btn_desc_game = QPushButton('游戏说明')
        self.btn_desc_game.setFocusPolicy(Qt.NoFocus)
        self.btn_exit = QPushButton('退出游戏')
        self.btn_exit.setFocusPolicy(Qt.NoFocus)
        self.hl.addWidget(self.btn_start_game)
        self.hl.addWidget(self.btn_desc_game)
        self.hl.addWidget(self.btn_exit)

        self.vl_bottom = QVBoxLayout(self)
        self.score_label = QLabel("得分：0")
        self.vl_bottom.addWidget(self.score_label)

        self.vl.addLayout(self.hl)
        self.game_window = GameWindow(self.myParam)
        self.game_window.setFocusPolicy(Qt.StrongFocus)
        self.game_window.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.vl.addWidget(self.game_window)
        self.vl.addLayout(self.vl_bottom)
        self.setLayout(self.vl)

        self.btn_start_game.clicked.connect(self.startGame)
        self.btn_desc_game.clicked.connect(self.descGame)
        self.btn_exit.clicked.connect(self.exitGame)
        # 得分改变信号
        self.game_window.scoreChangeSig.connect(self.handleScoreChangeSig)
        # 游戏状态改变信号
        self.game_window.gameStatusSig.connect(self.handleGameStatusSig)

    @pyqtSlot(int)
    def handleScoreChangeSig(self, score):
        print("score: " + str(score))
        self.score_label.setText("得分：" + str(score))

    @pyqtSlot(int)
    def handleGameStatusSig(self, status):
        print("game status change: " + str(status))
        if status == GAME_STATUS.GAME_RUNNING:
            self.btn_start_game.setText("暂停游戏")
        elif status == GAME_STATUS.GAME_PAUSE:
            self.btn_start_game.setText("开始游戏")
        elif status == GAME_STATUS.GAME_RESET:
            self.btn_start_game.setText("开始游戏")
            self.score_label.setText("得分：0")
            self.handleScoreChangeSig(0)

    def startGame(self):
        print("start game")
        if self.myParam.gameStatus == GAME_STATUS.GAME_RUNNING:
            self.myParam.gameStatus = GAME_STATUS.GAME_PAUSE
        elif self.myParam.gameStatus == GAME_STATUS.GAME_PAUSE:
            self.myParam.gameStatus = GAME_STATUS.GAME_RUNNING
        self.handleGameStatusSig(self.myParam.gameStatus)

    def descGame(self):
        print("desc game")
        self.descDialog = DescDialog()
        self.descDialog.show()

    def exitGame(self):
        print("exit game")
        # sys.exit(0)
        QApplication.exit(0)


if __name__ == "__main__":
    # 高分辨率缩放
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    e = MainWindow()
    apply_stylesheet(app, theme='dark_teal.xml')

    # 开屏画面
    splash = QSplashScreen()
    splash.setPixmap(QPixmap(r'img/start.jpg'))  # 设置背景图片
    splash.show()
    app.processEvents()
    time.sleep(2)

    e.show()
    splash.finish(e)
    sys.exit(app.exec_())
